<?php

Configure::load("gallery");

class GalleryController extends AppController {

 var $uses = array("Photo", "PhotoCategory");

 public function __construct() {
	 parent::__construct();
 }

 public function index() {
	 $categories = $this->PhotoCategory->find('all');
	 //debug($categories);
	 $this->set("categories", $categories);
 }

 public function category($id) {
	 $category = $this->PhotoCategory->findById($id);
	 //debug($category);
	 $this->set("category", $category);
 }

 public function admin_index() {
	 $categories = $this->PhotoCategory->find('all');
	 //debug($categories);
	 $this->set("categories", $categories);
 }

 public function admin_edit_category($id) {
	 $category = $this->PhotoCategory->findById($id);
	 //debug($category);
	 $this->set("category", $category);
 }

 public function admin_rename_category($id) {
	 $this->PhotoCategory->id = $id;
	 if (empty($this->data)) {
		 $this->data = $this->PhotoCategory->read();
	 } else {
		 if ($this->PhotoCategory->save($this->data)) {
			 $this->Session->setFlash('Zapisano kategorię.');
			 $this->redirect(array('action' => 'admin_index', $id));
		 }
	 }
 }

 public function admin_delete_category($id) {
	 $this->title = "Usuwanie kategorii zdjęć";

	 $photos = $this->Photo->findAllByCategoryId($id);

	 foreach ($photos as $photo) {
		 $sourceFilepath = Configure::read("Gallery.photos_dir") . "/" . $photo['Photo']['id'] . ".jpg";
		 $thumbnailFilepath = Configure::read("Gallery.photos_dir") . "/" . $photo['Photo']['id'] . "_small.jpg";
		 if (file_exists($sourceFilepath)) {
			 unlink($sourceFilepath);
		 }
		 else {
			 CakeLog::write('error', 'Couldnt delete image file: '.$sourceFilepath.'. File doesnt exist.');
		 }
		 
		 if (file_exists($thumbnailFilepath)) {
			 unlink($thumbnailFilepath);
		 }
		 else {
			 CakeLog::write('error', 'Couldnt delete image thumbnail file: '.$thumbnailFilepath.'. File doesnt exist.');
		 }
	 }

	 if ($this->PhotoCategory->delete($id)) {
		 $this->Session->setFlash('Usunięto.');
		 $this->redirect(array('action' => 'admin_index'));
	 }
 }

 public function admin_create_category() {
	 $this->title = "Nowa kategoria zdjęć";

	 if (!empty($this->data)) {
		 if ($this->News->save($this->data)) {
			 $this->Session->setFlash('Zapisano newsa.');
			 $this->redirect(array('action' => 'index'));
		 }
	 }
 }

 public function admin_delete_photo($id) {
	 $this->title = "Usuwanie zdjęcia";

	 if ($this->Photo->delete($id)) {
		 $this->Session->setFlash('Usunięto.');
		 $this->redirect(array('action' => 'admin_index'));
	 }
 }

 public function admin_add_photo($category) {
	 $this->title = "Dodaj zdjęcie";

	 if (!empty($this->data)) {
		 if ($this->Photo->save($this->data)) {
			 $this->Session->setFlash('Zapisano newsa.');
			 $this->redirect(array('action' => 'admin_view_category', $this->data['Photo']['category_id']));
		 }
	 }
 }

 private function _admin_rotate_photo($id, $degrees) {
	 $sourceFilepath = Configure::read("Gallery.photos_dir") . "/" . $id . ".jpg";
	 $source = imagecreatefromjpeg($sourceFilepath);
	 $rotate = imagerotate($source, $degrees, 0);
	 imagejpeg($rotate, $image, 98); //to file
	 generate_thumbnail($id);
 }

 public function admin_rotate_photo_left($id) {
	 _admin_rotate_photo($id, -90);
 }

 public function admin_rotate_photo_right($id) {
	 _admin_rotate_photo($id, 90);
 }

 public function generate_thumbnail($id) {
	 $this->layout = false;
	 $this->autoRender = false;

	 header("Content-type: image/jpeg");

	 $thumbnailSize = Configure::read("Gallery.thumbnail_size");
	 $sourceFilepath = Configure::read("Gallery.photos_dir") . "/" . $id . ".jpg";
	 $thumbnailFilepath = Configure::read("Gallery.photos_dir") . "/" . $id . "_small.jpg";

	 $sourceImage = imagecreatefromjpeg($sourceFilepath);
	 $sourceImageWidth = imagesx($sourceImage);
	 $sourceImageHeight = imagesy($sourceImage);

        $thumbnailImage = imagecreatetruecolor($thumbnailSize, $thumbnailSize);
        $black = imagecolorallocate($thumbnailImage, 0, 0, 0);
        imagefill($thumbnailImage, 0, 0, $black);

        if ($sourceImageWidth > $sourceImageHeight) {
            $newHeight = round(($sourceImageHeight * $thumbnailSize) / $sourceImageWidth);
            imagecopyresampled($thumbnailImage, $sourceImage, 0, ($thumbnailSize / 2 - $newHeight / 2), 0, 0, $thumbnailSize, $newHeight, $sourceImageWidth, $sourceImageHeight);
        } else {
            $newWidth = round(($sourceImageWidth * $thumbnailSize) / $sourceImageHeight);
            imagecopyresampled($thumbnailImage, $sourceImage, ($thumbnailSize / 2 - $newWidth / 2), 0, 0, 0, $newWidth, $thumbnailSize, $sourceImageWidth, $sourceImageHeight);
        }

        imagejpeg($thumbnailImage, $thumbnailFilepath, 86);
        imagejpeg($thumbnailImage, null, 86);

        imagedestroy($thumbnailImage);
        imagedestroy($sourceImage);
    }

}

?>
